package com.sokcet.io;

import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketConfig;
import com.corundumstudio.socketio.SocketIOServer;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * Created by 石头哥哥 on 8/20/16.
 * PACKAGE_NAME:  com.sokcet.io
 * COMMENT :
 */
@Component
public class ServerSocketIoService {


    @Bean
    public Configuration configuration() {
        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(3010);
        config.setBossThreads(1);
        config.setWorkerThreads(Runtime.getRuntime().availableProcessors() * 2);
        SocketConfig socketConfig = new SocketConfig();
        socketConfig.setTcpKeepAlive(true);        // 如果自己实现心跳机制,这里 则不开启 默认的Keepalive超时需要7,200,000 milliseconds，即2小时，探测次数为5次
        socketConfig.setReuseAddress(true);//地址复用
        socketConfig.setTcpNoDelay(true);
        socketConfig.setTcpSendBufferSize(512);
        socketConfig.setTcpReceiveBufferSize(512);
        config.setSocketConfig(socketConfig);
        config.setUseLinuxNativeEpoll(false);
        config.setMaxFramePayloadLength(1024 * 1024);
        config.setMaxHttpContentLength(1024 * 1024);
        return config;
    }

    @Bean
    public SocketIOServer socketIOServer() {
        return new SocketIOServer(configuration());
    }


    @Bean
    public ServerSocketIoHandler serverSocketIoHandler() {
        return new ServerSocketIoHandler(socketIOServer());
    }


    /**
     * 启动socketio网络模块
     */
    public void statrtServerSocketIo() {
        serverSocketIoHandler().start();
    }


}
